# -*- coding: UTF-8 -*-

import MySQLdb,psutil,sys


def addMe(con,m):
    cursor = con.cursor()
    sql = 'insert into memca(type,type_name,total,available,percent,free,active,inactive,buffers,cached,createDate) values (1,"内存","%s","%s","%s","%s","%s","%s","%s","%s",now())'
    try:
        # 执行sql语句
        cursor.execute(sql % (m.total,m.available,m.percent,m.free,m.active,m.inactive,m.buffers,m.cached))
        # 提交到数据库执行
        con.commit()
    except:
        # Rollback in case there is any error
        con.rollback()
        # 关闭数据库连接
        con.close()
if __name__ =='__main__':
    p = psutil.virtual_memory()
    m = Memca(p.total,p.available,p.percent,p.free,p.active,p.inactive,p.buffers,p.cached)
    con = GetCon('localhost','root','123456','sys_info')
    addMe(con.getCon(),m)
    print('ok.....')